package com.example.demo2.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.joda.time.DateTime;
import org.joda.time.LocalDateTime;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;

/**
 * @Author 胡亚伟
 * @Date 2025-06-11
 * @version 1.0
 * @注释
 */
public class Dates {
    private static Log log = LogFactory.get();
    public static final String yyyyMM = "yyyy-MM";
    public static final String yyyyMMdd = "yyyy-MM-dd";
    public static final String yyyyMMddHHmm = "yyyy-MM-dd HH:mm";
    public static final String yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";
    public static final String yyyyMMddHHmmssNoSplit = "yyyyMMddHHmmss";
    public static final String yyyyMMddHHNoSplit = "yyyyMMddHH";
    public static final String yyyyMMddNoSplit = "yyyyMMdd";
    public static final String yyyyMMNoSplit = "yyyyMM";

    static SimpleDateFormat formatyyyyMM = new SimpleDateFormat(yyyyMM);
    static SimpleDateFormat formatyyyyMMdd = new SimpleDateFormat(yyyyMMdd);
    static SimpleDateFormat formatyyyyMMddHHmm = new SimpleDateFormat(yyyyMMddHHmm);
    static SimpleDateFormat formatyyyyMMddHHmmss = new SimpleDateFormat(yyyyMMddHHmmss);
    static SimpleDateFormat formatyyyyMMddHHmmssNoSplit = new SimpleDateFormat(yyyyMMddHHmmssNoSplit);
    static DateTime dateTime = new DateTime();
    public static DateTimeFormatter formatter_yyyyMM = DateTimeFormatter.ofPattern(yyyyMM);

    /**
     * 按.net DateTime语义实现最大最小日期
     */
    public static final DateTime MaxDateTime = new DateTime(9999, 12, 31, 23, 59, 59, 999);
    public static final DateTime MinDateTime = new DateTime(0001, 1, 1, 0, 0, 0, 0);

    public static String format_yyyyMM(Date date) {
        if (date == null) {
            return null;
        }
        return formatyyyyMM.format(date);
    }

    public static String format_yyyyMMdd(Date date) {
        if (date == null) {
            return null;
        }
        return formatyyyyMMdd.format(date);
    }

    public static Date parse_yyyyMMdd(String date) {
        if (Strings.isNullOrEmpty(date)) {
            return null;
        }
        try {
            return formatyyyyMMdd.parse(date);
        } catch (ParseException e) {
            log.error(e, "日期解析异常，data:{}", date);
            return null;
        }
    }

    public static String format_yyyyMMddHHmmss(Date date) {
        return formatyyyyMMddHHmmss.format(date);
    }

    public static String format_yyyyMMddHHmm(Date date) {
        return formatyyyyMMddHHmm.format(date);
    }

    public static String format_yyyyMMddHHmmssNoSplie(Date date) {
        return formatyyyyMMddHHmmssNoSplit.format(date);
    }

    public static Date parse_yyyyMMddHHmmss(String date) {
        try {
            return formatyyyyMMddHHmmss.parse(date);
        } catch (ParseException e) {
            return null;
        }
    }

    public static Date parse_yyyyMMddHHmm(String date) {
        try {
            return formatyyyyMMddHHmm.parse(date);
        } catch (ParseException e) {
            log.error(e, "日期解析异常，data:{}", date);
            return null;
        }
    }

    public static Date parseOfFormat(String date, String format) {
        try {
            return new SimpleDateFormat(format).parse(date);
        } catch (ParseException e) {
            log.error(e, "日期解析异常，data:{}, format:{}", date, format);
            return null;
        }
    }

    public static String formatNowOfPattern(String format) {
        return LocalDateTime.now().toString(format);
    }

    public static String formatOfPattern(Date date, String format) {
        return LocalDateTime.fromDateFields(date).toString(format);
    }

    public static SimpleDateFormat getFormatyyyyMMddHHmmss() {
        return formatyyyyMMddHHmmss;
    }

    /**
     * 获取一天的00:00:00
     */
    public static Date getStartOfDate(Date date) {
        DateTime dateTime = org.joda.time.LocalDateTime.fromDateFields(date).toDateTime();
        return dateTime.millisOfDay().withMinimumValue().toDate();
    }

    /**
     * 获取一天的23:59:59
     */
    public static Date getEndOfDate(Date date) {
        DateTime dateTime = org.joda.time.LocalDateTime.fromDateFields(date).toDateTime();
        return dateTime.millisOfDay().withMaximumValue().toDate();
    }

    /**
     * 获取今天的00:00:00
     */
    public static Date getStartOfToday() {
        DateTime dateTime = DateTime.now();
        return dateTime.millisOfDay().withMinimumValue().toDate();
    }

    /**
     * 获取今天的23:59:59
     */
    public static Date getEndOfToday() {
        DateTime dateTime = DateTime.now();
        return dateTime.millisOfDay().withMaximumValue().toDate();
    }

    /**
     * 获取小时区间 00:00-01:00
     */
    public static String getHourInter(int hour) {
        int nextHour = hour + 1;
        return Strings.padStart("" + hour, 2, '0') + ":00-" + Strings.padStart("" + nextHour, 2, '0') + ":00";
    }

    // 上周日
    public static Date getLastWeekSunday() {

        Calendar date = Calendar.getInstance(Locale.CHINA);
        date.setFirstDayOfWeek(Calendar.MONDAY);// 将每周第一天设为星期一，默认是星期天
        date.add(Calendar.WEEK_OF_MONTH, -1);// 周数减一，即上周
        date.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);// 日子设为星期天
        return date.getTime();
    }

    public static String ignore_day = "day";
    public static String ignore_hour = "hour";
    public static String ignore_min = "min";
    public static String ignore_sec = "sec";

    /**
     * 两个时间相差距离多少天多少小时多少分多少秒
     *
     * @return String 返回值为：xx天xx时xx分xx秒 day hour min sec
     */
    public static String getDistanceTime(long millisecond, String... ignore) {
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;

        List<String> ignores = ignore != null ? Arrays.asList(ignore) : Lists.newArrayList();

        day = millisecond / (24 * 60 * 60 * 1000);
        hour = (millisecond / (60 * 60 * 1000) - day * 24);
        min = ((millisecond / (60 * 1000)) - day * 24 * 60 - hour * 60);
        sec = (millisecond / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        return (!ignores.contains(ignore_day) && day > 0 ? day + "天" : "")
                + (!ignores.contains(ignore_hour) && hour > 0 ? hour + "时" : "")
                + (!ignores.contains(ignore_min) && min > 0 ? min + "分" : "")
                + (!ignores.contains(ignore_sec) && sec > 0 ? sec + "秒" : "");
    }

    public static String getDayOfWeek(Date date) {
        LocalDateTime runStartDateTime = LocalDateTime.fromDateFields(date);
        int week = runStartDateTime.getDayOfWeek();
        if (week == 1) {
            return "周一";
        } else if (week == 2) {
            return "周二";
        } else if (week == 3) {
            return "周三";
        } else if (week == 4) {
            return "周四";
        } else if (week == 5) {
            return "周五";
        } else if (week == 6) {
            return "周六";
        } else if (week == 7) {
            return "周日";
        }
        return "";
    }

}